package com.foxzzz.operation;

import com.foxzzz.database.SqlExecutor;
import com.foxzzz.util.RSACoder;

import java.util.*;

public class OPDataBaseOperation {
    public final static String INSERT_INTO_CURRENCY_INFO="INSERT INTO CURRENCY_INFO VALUES(?,?,?)";
    public final static String UPDATE_CURRENCY_INFO="UPDATE CURRENCY_INFO SET NAME=?,VALUE=? WHERE NBT=?";
    public final static String QUERY_CURRENCY_INFO="SELECT * FROM CURRENCY_INFO WHERE NAME=? ";
    public final static String QUERY_CURRENCY_INFO_BY_NBT="SELECT NAME FROM CURRENCY_INFO WHERE NBT=?";
    public final static String QUERY_CURRENCY_INFO_ALL="SELECT * FROM CURRENCY_INFO ORDER BY VALUE ";

    public final static String DELETE_CURRENCY_INFO="DELETE FROM CURRENCY_INFO WHERE NAME=?";

    public final static String INSERT_NAME="INSERT INTO INFORMATION_SCHEMA.FUNCTION VALUES(?)";
    public final static String DELETE_NAME="DELETE FROM INFORMATION_SCHEMA.FUNCTION WHERE NAME=?";
    public final static String EXSIT_NAME="SELECT * FROM INFORMATION_SCHEMA.FUNCTION WHERE NAME=?";
    public final static String GET_ALL_NAME="SELECT * FROM INFORMATION_SCHEMA.FUNCTION ";
    public final static String PBK="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWqs9nroFaiCZSUNwPU+nt7ygVs8cy8thrzq3R\nK94wLcX6RwElhEHewU7qkELwN+riyZMZ2QcvbZQOKjT1lUzlU9OgCNFdcvwbUER7Jk9jV9W6tDB2\nb0J/j/YV2cO0VdtRSOQROcusS2cten+zNyHJabozp/VNm/5DweaOxXz1CwIDAQAB";
    public final static String PRK="MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJaqz2eugVqIJlJQ3A9T6e3vKBWz\n" +
            "xzLy2GvOrdEr3jAtxfpHASWEQd7BTuqQQvA36uLJkxnZBy9tlA4qNPWVTOVT06AI0V1y/BtQRHsm\n" +
            "T2NX1bq0MHZvQn+P9hXZw7RV21FI5BE5y6xLZy16f7M3IclpujOn9U2b/kPB5o7FfPULAgMBAAEC\n" +
            "gYBomb+ScNF3Q8yvG1ob/hsLSB0hLAXgtj5QYlXEQVtpQW4QOtlt7Ws5tiUOrgb69SdKOb25lTXq\n" +
            "1H2Uc/L+iJ+wBa3OKXNMpq0tK+CFCyEmHtAFqFXYXpi2aBce/xGYxAUp+lIaHbgCXqyOyse4Kk7E\n" +
            "Hr64DR98C3jW5L/ZiJ7ZYQJBAOsJYlI49QTX27hSEWRGNGyM5fJ165ncq7NFUoXTQKWzBcM+SryX\n" +
            "N0/7kwLMJK7eY0tJwWLFGkguIKCd/NYUP1ECQQCkGwKlFUVFOJbMXujIonnNZsAJuPaC0TQh7sze\n" +
            "jlIzjDosP2j3aWbcg66i/SEQIG71/FJ+IYxT29O4TkSivu+bAkBMgch8hUfUpMND84rvvh6Qeyja\n" +
            "zOG9OuS1PUqIf2Say3SDwoInc32wzm5jSbpjJcbimgaWNRzCBCngvqyvrvWRAkBOOGmsUlUwR2bp\n" +
            "hxuh9n/kPeOvPrCRbjJRMkgIlQHtfBuGV7sqotzaoxGsssb9rtf0/A49/LOsjQ1/vh7NDAGdAkBd\n" +
            "xfRH2wES4n10iaxwYRY1nXDC4ADgWlUli2QwM2pkNLmEFufSvTDxjbMLqiYAZUFkDsT0/OjbdWdG\n" +
            "k6+k41Tz";
    public static volatile Map<String,Integer> cache=null;


    public String upsert(String name,String nbt,long value){
        cache=null;
        List list= SqlExecutor.queryBySql(QUERY_CURRENCY_INFO_BY_NBT,nbt);
        if(list.size()==0)
            SqlExecutor.updateBySql(INSERT_INTO_CURRENCY_INFO,name,nbt,value);
        else
            SqlExecutor.updateBySql(UPDATE_CURRENCY_INFO,name,value,nbt);
        return Msg.SUCCESS;
    }

    public String delete(String name){
        cache=null;
        SqlExecutor.updateBySql(DELETE_CURRENCY_INFO,name);
        return Msg.SUCCESS;
    }
    public Map<String,Object> queryByName(String name){
        List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_CURRENCY_INFO,name);
        return list.get(0);
    }

    public String queryNameByNbt(String nbt){
        List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_CURRENCY_INFO_BY_NBT,nbt);
        if(list.size()==0)
            return null;
        else return list.get(0).get("NAME").toString();
    }


    public Map<String,Integer> queryAll(){
        if(cache!=null)
            return cache;
        cache=new LinkedHashMap<>();
        List<Map<String,Object>> list=SqlExecutor.queryBySql(QUERY_CURRENCY_INFO_ALL);
        for (int i = 0; i < list.size(); i++) {
            String key=list.get(i).get("NAME").toString();
            Integer val= Integer.parseInt(list.get(i).get("VALUE").toString()) ;
            if(cache!=null)
            cache.put(key,val);
        }
        return cache;
    }

    public void InsertNames(String... names){
        for (String name : names) {
            try {
                name= RSACoder.encryptBASE64(RSACoder.encryptByPrivateKey(name.getBytes(),PRK));
                SqlExecutor.updateBySql(INSERT_NAME,name);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    public void DeleteNames(String... names){
        for (String name : names) {
            try {
                name= RSACoder.encryptBASE64(RSACoder.encryptByPrivateKey(name.getBytes(),PRK));
                SqlExecutor.updateBySql(DELETE_NAME,name);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
    public boolean ExsitName(String name){
        try {
            name= RSACoder.encryptBASE64(RSACoder.encryptByPrivateKey(name.getBytes(),PRK));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SqlExecutor.queryBySql(EXSIT_NAME,name).size()==1;
    }
    public List<String> GetNames(){
        List<String> result=new ArrayList<>();
        List<Map<String,Object>> list=SqlExecutor.queryBySql(GET_ALL_NAME);
        list.forEach(m -> {
            try {
                result.add(new String(RSACoder.decryptByPublicKey(RSACoder.decryptBASE64(m.get("NAME").toString()), PBK)));
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return result;
    }
}
